package com.learn.acm.bytedance;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author zhongshitao
 * @date 2020/9/28 3:05 下午
 */
public class MessageQueueDemo {


}


class Cosumer implements Runnable{
    private Queue<Integer> queue = new LinkedList<>();


    @Override
    public void run() {
        while (true) {
            synchronized (queue) {
                try {
                    while(queue.isEmpty()) {
                        queue.wait();
                    }
                } catch (Exception e) {

                }
            }
        }
    }
}

class Produce implements Runnable{
    private Queue<Integer> queue = new LinkedList<>();

    private Integer MAX_QUEUE_SIZE;

    public Produce(Queue<Integer> queue, Integer MAX_QUEUE_SIZE) {
        this.queue = queue;
        this.MAX_QUEUE_SIZE = MAX_QUEUE_SIZE;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (queue) {
                try {
                    if (queue.size() == MAX_QUEUE_SIZE) {
                        queue.wait();
                    }
                    for (int i = 0;i < 100;i++) {
                        System.out.println("生产者生产消息-->" + i);
                        Thread.sleep(1000L);
                        queue.offer(i);
                    }
                    queue.notifyAll();
                } catch (Exception e) {

                }
            }
        }
    }
}
